﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Diagnostics;


namespace WpfTimer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {     
        private DispatcherTimer _countdownTimer; 
        private TimeSpan _target;
        private String _mp3File;

        private MediaPlayer _player = new MediaPlayer();
        
        public MainWindow()
        {
            InitializeComponent();

            SetupTimer();
            SetupMp3File();
        }

        #region Setup

        private void SetupMp3File()
        {
            _mp3File = System.IO.Directory.GetCurrentDirectory().ToString() + Properties.Resources.strSirenPath;

        }

        private void SetupTimer()
        {
            _countdownTimer = new DispatcherTimer();
            _countdownTimer.Interval = new TimeSpan(0, 0, 1);
            _countdownTimer.Tick += new EventHandler(CountdownTimerStep);
        }

        #endregion

        #region ButtonActions

        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _target = TimeSpan.Parse(this.textBox1.Text.ToString());
                _countdownTimer.Start();
                this.buttonReset.Visibility = Visibility.Hidden;
            }

            catch (System.FormatException)
            {
                MessageBox.Show("Bitte Zeitformat hh:mm:ss einhalten");
                ResetTimer();
            }

            catch (System.Exception excep)
            {
                MessageBox.Show(excep.Message);
                ResetTimer();
            }

        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.Stop();
            this.buttonReset.Visibility = Visibility.Visible;
        }

        private void buttonReset_Click(object sender, RoutedEventArgs e)
        {
            ResetTimer();
        }

        #endregion


        #region Helper

        private void CountdownTimerStep(object sender, EventArgs e)
        {

            if (_target.TotalSeconds > 0)
            {
                _target = _target.Subtract(_countdownTimer.Interval);
                this.textBox1.Text = String.Format("{0:hh}:{0:mm}:{0:ss}", _target);
                this.Title = String.Format("{0:hh}:{0:mm}:{0:ss}", _target);

            }
            else
            {
                _countdownTimer.Stop();
                this.Activate();
                PlayAlert(_mp3File);
                this.Title = "WPFTimer";
            }

        }

        private void PlayAlert(string _mp3File)
        {
            _player.Open(new Uri(_mp3File, UriKind.Absolute));
            _player.Play();
            //Debug.WriteLine("Start Playback");
            //Debug.WriteLine(_player.NaturalDuration.HasTimeSpan.ToString());
            //Debug.WriteLine(_player.NaturalDuration.TimeSpan.ToString());

            System.Threading.Thread.Sleep(5000);
            _player.Close();

        }
        

        private void ResetTimer()
        {
                    this.textBox1.Text = "00:00:00";
                    this.Title = "WPFTimer";
        }

        #endregion

    }
}
